package ig.metrica.ejb.conbalance;

import ig.util.ejb.IGBean;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.ejb.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 * This is the bean class for the ConfiguracionBalanceSBBean enterprise bean.
 * Created 12/12/2006 11:23:40 AM
 * @author jsanchez
 */
public class ConfiguracionBalanceSBBean implements SessionBean, ConfiguracionBalanceSBRemoteBusiness, ConfiguracionBalanceSBLocalBusiness {
    private SessionContext context;
    
    // <editor-fold defaultstate="collapsed" desc="EJB infrastructure methods. Click the + sign on the left to edit the code.">
    // TODO Add code to acquire and use other enterprise resources (DataSource, JMS, enterprise bean, Web services)
    // TODO Add business methods or web service operations
    /**
     * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
     */
    public void setSessionContext(SessionContext aContext) {
        context = aContext;
    }
    
    /**
     * @see javax.ejb.SessionBean#ejbActivate()
     */
    public void ejbActivate() {
        
    }
    
    /**
     * @see javax.ejb.SessionBean#ejbPassivate()
     */
    public void ejbPassivate() {
        
    }
    
    /**
     * @see javax.ejb.SessionBean#ejbRemove()
     */
    public void ejbRemove() {
        
    }
    // </editor-fold>
    
    /**
     * See section 7.10.3 of the EJB 2.0 specification
     * See section 7.11.3 of the EJB 2.1 specification
     */
    public void ejbCreate() {
        // TODO implement ejbCreate if necessary, acquire resources
        // This method has access to the JNDI context so resource aquisition
        // spanning all methods can be performed here such as home interfaces
        // and data sources.
    }
    
    // Add business logic below. (Right-click in editor and choose
    // "EJB Methods > Add Business Method" or "Web Service > Add Operation")
    public Collection findByModelo(Integer idModelo)  throws java.lang.Exception {
        Connection MetricaDW=null;
        PreparedStatement prep=null;
        Collection CC=new ArrayList();
        ResultSet res=null;
        try {
            MetricaDW=this.getConexion("METRICADW");
            
            prep=MetricaDW.prepareStatement("SELECT IDMODELO, PUC, DURACION, PROCENTAJE FROM CONFIGURACIONBALANCES WHERE IDMODELO = ?");
            prep.setInt(1,idModelo);
            res=prep.executeQuery();
            while(res.next()){
                CC.add(
                        new ConfiguracionbalancesDTO(
                        res.getInt("IDMODELO"),
                        res.getInt("PUC"),
                        res.getInt("DURACION"),
                        res.getInt("PROCENTAJE")
                        )
                        );
            }
            prep.close();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {prep.close();} catch (Exception e) {}
            try {MetricaDW.close();} catch (Exception e) {}
            try {res.close();} catch (Exception e) {}
        }
        return CC;
    }
    public void crear(Collection c) throws java.lang.Exception {
        //TODO implement crear
//        boolean eliminar=true;
        Connection MetricaDW=null;
        PreparedStatement prep=null;
        try {
            MetricaDW=this.getConexion("METRICADW");
            for (Iterator it = c.iterator(); it.hasNext();) {
                ConfiguracionbalancesDTO elem = (ConfiguracionbalancesDTO) it.next();
//                if(eliminar){
//                    this.eliminar(elem.getIdmodelo());
//                    eliminar=false;
//                }
                prep=MetricaDW.prepareStatement("INSERT INTO CONFIGURACIONBALANCES(IDMODELO, PUC, DURACION, PROCENTAJE) VALUES(?, ?, ?, ?)");
                prep.setInt(1,elem.getIdmodelo());
                prep.setInt(2,elem.getPuc());
                prep.setInt(3,elem.getDuracion());
                prep.setInt(4,elem.getProcentaje());
                prep.executeUpdate();
                prep.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {prep.close();} catch (Exception e) {}
            try {MetricaDW.close();} catch (Exception e) {}
        }
    }
    
    public void eliminar(Integer idModelo) throws java.lang.Exception {
        Connection MetricaDW=null;
        PreparedStatement prep=null;
        try {
            MetricaDW=this.getConexion("METRICADW");
            prep=MetricaDW.prepareStatement("DELETE FROM CONFIGURACIONBALANCES WHERE IDMODELO = ?");
            prep.setInt(1,idModelo);
            prep.executeUpdate();
            prep.close();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {prep.close();} catch (Exception e) {}
            try {MetricaDW.close();} catch (Exception e) {}
        }
    }
    
    public Connection getConexion(String datasource) throws NamingException, SQLException{
        
        return new IGBean().getConexion(datasource);
        /*Context ctx = new InitialContext();
        DataSource ds = (DataSource)ctx.lookup("java:/"+datasource);        
        Connection c=  ds.getConnection();
        return c;*/
    }
    
}
